package com.dd.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dd.entity.DdShop;
import com.dd.entity.DdShopgoods;
import com.dd.entity.DdSms;
import com.dd.entity.DdSystemuser;
import com.dd.entity.DdUser;
import com.dd.entity.DdUserpage;
import com.dd.entity.DdUserpay;
import com.dd.util.DateStrFormat;
import com.dd.util.PageList;
import com.dd.util.PubFun;

public class AppUserAction extends BaseAction {
	public ActionForward test(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {
			File f = new File("c:/1.txt");
			BufferedReader reader = null;
	        try {
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader = new BufferedReader(new FileReader(f));
	            String tempString = null;
	            int line = 1;
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	                // 显示行号
	            	this.getDdShopDAO().test(tempString);
	                System.out.println("line " + line + ": " + tempString);
	                line++;
	            }
	            reader.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }

			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
	public ActionForward userinfo(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {
	
			HttpSession session = request.getSession();
			if(session.getAttribute("logphone")==null){
				out.print("{\"result\":-1,\"msg\":\"登录超时!\"}");
				out.flush();
				out.close();
				return null;
			}
			String phone = (String)session.getAttribute("logphone");	
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			out.print("{\"logid\":\"" + du.getLogid() + "\" ,\"qq\":\"" + du.getQq()
					+ "\",\"weibo\":\"" + du.getWeibo() + "\",\"name\":\""
					+ du.getName() + "\",\"address\":\"" + du.getAddress()
					+ "\",\"sex\":\"" + du.getSex() + "\",\"email\":\""
					+ du.getEmail() + "\",\"phone\":\"" + du.getPhone()
					+ "\",\"score\":\"" + du.getScore() + "\",\"birthday\":\""
					+ DateStrFormat.getCurrentTime1(du.getBirthday()) + "\"}");
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward reguser(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {
			String phone = request.getParameter("phone");
			String name = request.getParameter("name");
			String sex = request.getParameter("sex");			
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			if (du != null) {
				out.print("{\"result\":0,\"msg\":\"电话号码已经注册过了\"}");
				out.flush();
				out.close();
				return null;
			}
			du = new DdUser();
			du.setLogid(phone);
			du.setPassword(new Random().nextInt(9999999)+123456+"'");
			du.setStatus(1);
			du.setName(name);	
			du.setSex(Integer.parseInt(sex));
			du.setCredit(1);
			du.setPhone(phone);
			du.setRegdate(new Date());
			du.setCode((new Random().nextInt(500000)+100000)+"");
			du.setLastsend(System.currentTimeMillis());
			//加一条短信
			this.getDdUserDAO().saveOrUpdate(du);
			DdSms dsms = new DdSms();
			dsms.setSendmsg("感谢您注册蛋订网,您的验证码是:"+du.getCode());
			dsms.setOrderid(0);
			dsms.setOrderstatus(1);
			dsms.setTypes(2);
			dsms.setStatus(0);
			dsms.setPhone(phone);
			this.getDdSmsDAO().saveOrUpdate(dsms);
			// int code = new Random().nextInt(500000)+100000;
			// System.out.println(code);
			out.print("{\"result\":1,\"msg\":\"验证码已经发送!验证码是:"+du.getCode()+"\"}");
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward recode1(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {
			String phone = request.getParameter("phone");		
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			if (du == null) {
				out.print("{\"result\":0,\"msg\":\"电话号码不存在!\"}");
				out.flush();
				out.close();
				return null;
			}
			if((System.currentTimeMillis()-du.getLastsend().longValue())<120000){
				out.print("{\"result\":0,\"msg\":\"短信发送太频繁,请稍后再试\"}");
				out.flush();
				out.close();
				return null;
			}
			du.setCode((new Random().nextInt(500000)+100000)+"");
			du.setLastsend(System.currentTimeMillis());
			//加一条短信
			this.getDdUserDAO().saveOrUpdate(du);
			DdSms dsms = new DdSms();
			dsms.setSendmsg("感谢您注册蛋订网,您的验证码是:"+du.getCode());
			dsms.setOrderid(0);
			dsms.setOrderstatus(1);
			dsms.setTypes(2);
			dsms.setStatus(0);
			dsms.setPhone(phone);
			this.getDdSmsDAO().saveOrUpdate(dsms);
			out.print("{\"result\":1,\"msg\":\"验证码已重新发送!验证码是:"+du.getCode()+"\"}");
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
	//不传password表示只验证验证码是否正确
	public ActionForward setpassword(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		try {
			DdUser du = this.getDdUserDAO().findByPhone(
					request.getParameter("phone"));
			if (du == null) {
				out.print("{\"result\":0,\"msg\":\"电话号码不存在!\"}");
				out.flush();
				out.close();
				return null;
			}
			if (du.getCode().equals(request.getParameter("code"))) {
				if(request.getParameter("password")==null||request.getParameter("password").equals("")){
					out.print("{\"result\":1,\"msg\":\"验证码正确!\"}");
					return null;
				}else{
					du.setPassword(request.getParameter("password"));
					du.setLastlogin(new Date());
					this.getDdUserDAO().saveOrUpdate(du);
					session.setAttribute("logphone", du.getPhone());
					out.print("{\"result\":1,\"msg\":\"设置密码成功!\",\"jsessionid\":\""+session.getId()+"\"}");
					return null;
				}	
			}else{
				out.print("{\"result\":0,\"msg\":\"验证码不对!\"}");
				out.flush();
				out.close();
				return null;
			}
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward forgetpass(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		try {
			String phone = request.getParameter("phone");		
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			if (du == null) {
				out.print("{\"result\":0,\"msg\":\"电话号码不存在!\"}");
				out.flush();
				out.close();
				return null;
			}
			if((System.currentTimeMillis()-du.getLastsend().longValue())<120000){
				out.print("{\"result\":0,\"msg\":\"短信发送太频繁,请稍后再试\"}");
				out.flush();
				out.close();
				return null;
			}
			du.setCode((new Random().nextInt(500000)+100000)+"");
			du.setLastsend(System.currentTimeMillis());
			//加一条短信
			this.getDdUserDAO().saveOrUpdate(du);
			DdSms dsms = new DdSms();
			dsms.setSendmsg("您的验证码是:"+du.getCode());
			dsms.setOrderid(0);
			dsms.setOrderstatus(1);
			dsms.setTypes(2);
			dsms.setStatus(0);
			dsms.setPhone(phone);
			this.getDdSmsDAO().saveOrUpdate(dsms);
			out.print("{\"result\":1,\"msg\":\"验证短信发送成功!验证码是:"+du.getCode()+"\"}");
			out.flush();
			out.close();
			return null;
			
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	//重新发送忘记密码的验证码
	public ActionForward recode2(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {
			String phone = request.getParameter("phone");		
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			if (du == null) {
				out.print("{\"result\":0,\"msg\":\"电话号码不存在!\"}");
				out.flush();
				out.close();
				return null;
			}
			if((System.currentTimeMillis()-du.getLastsend().longValue())<120000){
				out.print("{\"result\":0,\"msg\":\"短信发送太频繁,请稍后再试\"}");
				out.flush();
				out.close();
				return null;
			}
			du.setCode((new Random().nextInt(500000)+100000)+"");
			du.setLastsend(System.currentTimeMillis());
			//加一条短信
			this.getDdUserDAO().saveOrUpdate(du);
			DdSms dsms = new DdSms();
			dsms.setSendmsg("您的验证码是:"+du.getCode());
			dsms.setOrderid(0);
			dsms.setOrderstatus(1);
			dsms.setTypes(2);
			dsms.setStatus(0);
			dsms.setPhone(phone);
			this.getDdSmsDAO().saveOrUpdate(dsms);
			out.print("{\"result\":1,\"msg\":\"验证码已重新发送!验证码是:"+du.getCode()+"\"}");
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	

	public ActionForward login(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		try {
			DdUser du = this.getDdUserDAO().findByPhone(
					request.getParameter("phone"));
			if (du == null) {
				out.print("{\"result\":0,\"msg\":\"电话号码不存在!\"}");
				out.flush();
				out.close();
				return null;
			}
			if (du.getPassword().equals(request.getParameter("password"))) {
				session.setAttribute("logphone", du.getPhone());
				out.print("{\"result\":1,\"msg\":\"登录成功!\",\"jsessionid\":\""+session.getId()+"\"}");
				return null;
			}else{
				out.print("{\"result\":0,\"msg\":\"密码不对!\"}");
				out.flush();
				out.close();
				return null;
			}
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward updatephone(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {
			String newphone = request.getParameter("newphone");
			HttpSession session = request.getSession();
			if(session.getAttribute("logphone")==null){
				out.print("{\"result\":-1,\"msg\":\"登录超时!\"}");
				out.flush();
				out.close();
				return null;
			}
			String phone = (String)session.getAttribute("logphone");
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			DdUser du2 = this.getDdUserDAO().findByPhone(newphone);
			if(du2!=null){
				out.print("{\"result\":0,\"msg\":\"新号码已经被注册过了!\"}");
				out.flush();
				out.close();
				return null;
			}
			du.setAddress2(newphone);
			du.setCode3((new Random().nextInt(500000)+100000)+"");
			du.setLastsend(System.currentTimeMillis());
			//加一条短信
			this.getDdUserDAO().saveOrUpdate(du);
			DdSms dsms = new DdSms();
			dsms.setSendmsg("您的验证码是:"+du.getCode3());
			dsms.setOrderid(0);
			dsms.setOrderstatus(1);
			dsms.setTypes(2);
			dsms.setStatus(0);
			dsms.setPhone(newphone);
			this.getDdSmsDAO().saveOrUpdate(dsms);
			out.print("{\"result\":1,\"msg\":\"验证码已重新发送!\"}");
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":-1}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	//重新发送修改手机号码的验证码
	public ActionForward recode3(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {	
			HttpSession session = request.getSession();
			if(session.getAttribute("logphone")==null){
				out.print("{\"result\":-1,\"msg\":\"登录超时!\"}");
				out.flush();
				out.close();
				return null;
			}
			String phone = (String)session.getAttribute("logphone");	
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			if((System.currentTimeMillis()-du.getLastsend().longValue())<120000){
				out.print("{\"result\":0,\"msg\":\"短信发送太频繁,请稍后再试\"}");
				out.flush();
				out.close();
				return null;
			}
			du.setCode3((new Random().nextInt(500000)+100000)+"");
			du.setLastsend(System.currentTimeMillis());
			//加一条短信
			this.getDdUserDAO().saveOrUpdate(du);
			DdSms dsms = new DdSms();
			dsms.setSendmsg("您的验证码是:"+du.getCode3());
			dsms.setOrderid(0);
			dsms.setOrderstatus(1);
			dsms.setTypes(2);
			dsms.setStatus(0);
			//用地址2缓存新号码
			dsms.setPhone(du.getAddress2());
			this.getDdSmsDAO().saveOrUpdate(dsms);
			out.print("{\"result\":1,\"msg\":\"验证码已重新发送!\"}");
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward confirmphone(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		try {
			if(session.getAttribute("logphone")==null){
				out.print("{\"result\":-1,\"msg\":\"登录超时!\"}");
				out.flush();
				out.close();
				return null;
			}
			String phone = (String)session.getAttribute("logphone");	
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			if(du.getCode3().equals(request.getParameter("code"))){
				du.setPhone(du.getAddress2());
				out.print("{\"result\":0,\"msg\":\"修改电话成功!\"}");
				out.flush();
				out.close();
				return null;
			}else{
				out.print("{\"result\":0,\"msg\":\"验证码不对!\"}");
				out.flush();
				out.close();
				return null;
			}
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
	//更新密码
	public ActionForward updatepassword(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {
			
			HttpSession session = request.getSession();
			if(session.getAttribute("logphone")==null){
				out.print("{\"result\":-1,\"msg\":\"登录超时!\"}");
				out.flush();
				out.close();
				return null;
			}
			String phone = (String)session.getAttribute("logphone");
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			String password = request.getParameter("password");
			if(!du.getPassword().equals(password)){
				out.print("{\"result\":0,\"msg\":\"旧密码不对!\"}");
			}
			
			du.setPassword(password);
			this.getDdUserDAO().saveOrUpdate(du);
			out.print("{\"result\":1,\"msg\":\"密码修改成功!\"}");
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":-1}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
	//修改其他个人信息
	public ActionForward updateuser(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {

			HttpSession session = request.getSession();
			if(session.getAttribute("logphone")==null){
				out.print("{\"result\":-1,\"msg\":\"登录超时!\"}");
				out.flush();
				out.close();
				return null;
			}
			String phone = (String)session.getAttribute("logphone");
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			String name = request.getParameter("name");
			String sex = request.getParameter("sex");
			String email = request.getParameter("email");
			String address = request.getParameter("address");
			String birthday = request.getParameter("birthday");
			if (name != null) {
				du.setName(name);
			}
			if (sex != null) {
				du.setSex(Integer.parseInt(sex));
			}
			if (address != null) {
				du.setAddress(address);
			}
			if (email != null) {
				du.setSex(Integer.parseInt(sex));
			}
			if (birthday != null) {
				du.setBirthday(DateStrFormat.getDateByString(birthday));
				
			}
			this.getDdUserDAO().saveOrUpdate(du);
			out.print("{\"result\":1,\"msg\":\"修改成功!\"}");
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
	
	//我的收藏夹
	public ActionForward myfav(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {
			
			HttpSession session = request.getSession();
			if(session.getAttribute("logphone")==null){
				out.print("{\"result\":-1,\"msg\":\"登录超时!\"}");
				out.flush();
				out.close();
				return null;
			}
			String phone = (String)session.getAttribute("logphone");
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			int skip=0;
			int count=10;
			if(request.getParameter("skip")!=null){
				skip=Integer.parseInt(request.getParameter("skip"));
			}
			if(request.getParameter("count")!=null){
				count=Integer.parseInt(request.getParameter("count"));
			}
			PageList list = this.getDdUserpageDAO().findByUserid(du.getId(),skip,count);
			StringBuffer sbf = new StringBuffer("{\"total\":"+list.getTotalCount()+",\"favs\":[");
			boolean isfirst = false;
			for(int i=0;i<list.size();i++){
				DdUserpage dup = (DdUserpage)list.get(i);
				DdShop ds = this.getDdShopDAO().findById(dup.getShopid());
				if(ds==null||ds.getAlive().intValue()==0){
					continue;
				}
				String des = "";
				if(ds.getIsbooking()==1&&ds.getDiscount()>0F&&ds.getDiscount()<10F){
					des = ds.getDiscount()+"折";
				}else if(ds.getIssend()==1){
					des = ds.getSendreq();
				}
				if(!isfirst){
					isfirst=true;
					sbf.append("{\"sid\":\""+ds.getId()+"\",\"sname\":\""+ds.getName()+"\",\"isbooking\":\""+ds.getIsbooking()+"\",\"iscoupon\":\""+ds.getIscoupon()+"\",\"issend\":\""+ds.getIssend()+"\",\"description\":\""+des+"\"}");
				}else{
					sbf.append(",{\"sid\":\""+ds.getId()+"\",\"sname\":\""+ds.getName()+"\",\"isbooking\":\""+ds.getIsbooking()+"\",\"iscoupon\":\""+ds.getIscoupon()+"\",\"issend\":\""+ds.getIssend()+"\",\"description\":\""+des+"\"}");
				}	
			}	
			sbf.append("]}");
			out.print(sbf.toString());
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
	//我的收藏夹
	public ActionForward addfav(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {
			
			HttpSession session = request.getSession();
			if(session.getAttribute("logphone")==null){
				out.print("{\"result\":-1,\"msg\":\"登录超时!\"}");
				out.flush();
				out.close();
				return null;
			}
			String phone = (String)session.getAttribute("logphone");
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			int sid = Integer.parseInt(request.getParameter("sid"));
			DdUserpage dup = this.getDdUserpageDAO().findByUserAndShopid(du.getId(), sid); 
			if(dup!=null){
				out.print("{\"result\":0,\"msg\":\"这个商家已经在收藏夹里面了!\"}");
				out.flush();
				out.close();
				return null;
			}
			DdShop ds = this.getDdShopDAO().findById(sid);
			if(ds==null||ds.getAlive().intValue()==0){
				out.print("{\"result\":0,\"msg\":\"这个商家已经下线了!\"}");
				out.flush();
				out.close();
				return null;
			}
			dup= new DdUserpage();
			dup.setShopid(sid);
			dup.setUserid(du.getId());
			dup.setPageindex(this.getDdUserpageDAO().findMaxIndex(du.getId()));
			this.getDdUserpageDAO().saveOrUpdate(dup);
			out.print("{\"result\":1,\"msg\":\"添加成功!\"}");
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
	//我的收藏夹
	public ActionForward delfav(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		try {
			
			HttpSession session = request.getSession();
			if(session.getAttribute("logphone")==null){
				out.print("{\"result\":-1,\"msg\":\"登录超时!\"}");
				out.flush();
				out.close();
				return null;
			}
			String phone = (String)session.getAttribute("logphone");
			DdUser du = this.getDdUserDAO().findByPhone(phone);
			int sid = Integer.parseInt(request.getParameter("sid"));
			DdUserpage dup = this.getDdUserpageDAO().findByUserAndShopid(du.getId(), sid); 
			if(dup!=null){
				this.getDdUserpageDAO().delete(dup);
			}
			
			out.print("{\"result\":1,\"msg\":\"删除成功!\"}");
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
}
